/**
 * @file Diag_OtaUDSConfig.c
 * @author dowson
 * @brief 
 * @version 0.1
 * @date 2023-08-17
 * 
 * @copyright Copyright (c) 2023
 * 
 */

/* Suppress warning messages */
#if defined(__CC_ARM)
// Suppress warning message: extended constant initialiser used
#pragma diag_suppress 1296
#elif defined(__ICCARM__)
#elif defined(__GNUC__)
#endif


#include "stdint.h"
#include "stdlib.h"
#include "string.h"
#include "string.h"

#include "Diag_UDSClient.h"
#include "App_OtaUDSUpgradeMain.h"
#include "system_init.h"
#include "OtaData.h"

#include "Diag_OtaUDSConfigAC08.h"

#ifdef PARTFROM_AC8

extern t_OtaStateManage OtaStateManage;
extern Diag_UDSClientType UDSClient;
extern uint8_t UDSKeyValue_0x27[4];

static uint8_t updateTime[8] = {0};

// static const uint8_t AC08_BMS_RoutineCtrlEndData[4] = {0xdb, 0x5d, 0x53, 0x12};
// sv=1
static const uint8_t AC08_BMS_RoutineCtrlEndData[4] = {0x5c, 0x11, 0x69, 0x5f};

static const uint8_t AC08_BMS_RoutineCtrlMiddleData[4] = {0xfb, 0xb6, 0x43, 0x94};
static const uint8_t AC08_BMS_RoutineCtrlContinueData[9] = {0x44, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x66, 0xa8};
static const uint8_t AC08_VCU_RoutineCtrlData[9]={0x44, 0x00, 0x50, 0x02, 0x00, 0x00, 0x04, 0x68, 0x28};
// static const uint8_t AC08_VCU_RoutineDeCtrlData[4]={0x2e, 0xf1, 0x69, 0xad};
// 9.21
static const uint8_t AC08_VCU_RoutineDeCtrlData[4]={0xb2, 0xd7, 0x42, 0xbd};

static const uint16_t AC08_BMS_ReadVersion[1] = {0xf187};
static const uint8_t AC08_OBC_RoutineCtrlContinueSecondData[4] = {0x8a, 0xfe, 0x1d, 0x8d};
static uint8_t AC08_OBC_RoutineCtrlContinueData[9] = {0x44};


Diag_ErrType Diag_Request_HeadSize_CallBack_AC8_OBC(Diag_UDSClientType *client){
    uint32_t S19size;
    uint32_t S19head;
    if(!client->RecvDataType.timeOutState){
        for(OtaStateManage.step_ecu_files = 0;OtaStateManage.step_ecu_files <= OTA_OTA_FILE;OtaStateManage.step_ecu_files++){
            if(OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].downloadEcuFileSizeSum != 0){
                break;
            }
        }
        S19head = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.BinHeadAddr;
        S19size = OtaStateManage.downloadFileState[OtaStateManage.step_ecu_files].binMsg.hasBinSize;

//        fprintf(USART2_STREAM,"get S19head:%x\n",S19head);
//        fprintf(USART2_STREAM,"get S19size:%x\n",S19size);

    	AC08_OBC_RoutineCtrlContinueData[1] = S19head >> 24;
    	AC08_OBC_RoutineCtrlContinueData[2] = (S19head >> 16) & 0xff;
    	AC08_OBC_RoutineCtrlContinueData[3] = (S19head >> 8) & 0xff;
    	AC08_OBC_RoutineCtrlContinueData[4] = S19head & 0xff;
    	AC08_OBC_RoutineCtrlContinueData[5] = S19size >> 24;
    	AC08_OBC_RoutineCtrlContinueData[6] = (S19size >> 16) & 0xff;
    	AC08_OBC_RoutineCtrlContinueData[7] = (S19size >> 8) & 0xff;
    	AC08_OBC_RoutineCtrlContinueData[8] = S19size & 0xff;
        client->RecvDataType.responseState = RequestResponseState(client->sid,client->recvBuf,client->recvSize);
    }else{
        client->RecvDataType.responseState = Diag_RESPONSE_ERR;
            return Diag_ERR_TIMEOUT;
    }
    return Diag_OK;
}


//鍒峰啓瀹屾垚璁剧疆鏃堕棿鍥炶皟
Diag_ErrType Diag_Request_SteTime_CallBack_AC8_OBC(Diag_UDSClientType *client){
    memset(updateTime,0x00,sizeof(updateTime));
    if(!client->RecvDataType.timeOutState){
        if(!GetSystemTime(updateTime)){
        	for(int i=6;i--;i>0){
        	    updateTime[i]=(updateTime[i-1]/10)*16+updateTime[i-1]%10;
        	}
        	updateTime[0] = 0x20;
            fprintf(USART2_STREAM,"get time:%d,%d,%d",updateTime[0],updateTime[1],updateTime[2]);
        }else{
            //TODO锛氳缃粯璁ゆ椂闂达紝1970锛�锛�

        }
        client->RecvDataType.responseState = RequestResponseState(client->sid,client->recvBuf,client->recvSize);
    }else{
        client->RecvDataType.responseState = Diag_RESPONSE_ERR;
            return Diag_ERR_TIMEOUT;
    }
    return Diag_OK;
}

Diag_ErrType Diag_Request_0x27_CallBack_AC8(Diag_UDSClientType *client){

    uint32_t UDSSeed = 0;
    uint32_t UDSKey = 0;
    uint8_t loopTimes = 32;
    memset(UDSKeyValue_0x27,0x00,sizeof(UDSKeyValue_0x27));
    //fprintf(USART2_STREAM,"-timeOutState:%d\r\n",client->RecvDataType.timeOutState);     
    if(!client->RecvDataType.timeOutState){
        //fprintf(USART2_STREAM,"-0x27-recvSize:%d\r\n",client->recvSize);    
        UDSSeed = (UDSSeed = ((UDSSeed = client->recvBuf[3])<<8 | client->recvBuf[4]) << 8 | client->recvBuf[5]) << 8 | client->recvBuf[6];
        //fprintf(USART2_STREAM,"get seed:%x,ecutype:%d,loopTime:%d",UDSSeed,client->ecuType,loopTimes);
        UDSKey = Diag_KeyComputation(UDSSeed,client->ecuType,loopTimes);
        //fprintf(USART2_STREAM,"get key:%x",UDSKey);
        UDSKeyValue_0x27[0] = (UDSKey >> 24) & 0xff;
        UDSKeyValue_0x27[1] = (UDSKey >> 16) & 0xff;
        UDSKeyValue_0x27[2] = (UDSKey >> 8) & 0xff;
        UDSKeyValue_0x27[3] = (UDSKey >> 0) & 0xff;   

        client->RecvDataType.responseState = RequestResponseState(client->sid,client->recvBuf,client->recvSize);
    }else{
        client->RecvDataType.responseState = Diag_RESPONSE_ERR;
            return Diag_ERR_TIMEOUT;
    }
    return Diag_OK;
}

//刷写完成设置时间回调
Diag_ErrType Diag_Request_SteTime_CallBack_AC8(Diag_UDSClientType *client){
    memset(updateTime,0x00,sizeof(updateTime));
    if(!client->RecvDataType.timeOutState){
        if(!GetSystemTime(updateTime)){
            for(int i=6; i>0; i--)
            {
                updateTime[i]=(updateTime[i-1]/10)*16+updateTime[i-1]%10;
            }
            updateTime[0] = 0x20;
            fprintf(USART2_STREAM,"get time:%d,%d,%d,%d",updateTime[0],updateTime[1],updateTime[2],updateTime[3]);
        }else{
            //TODO：设置默认时间，1970，1，1

        }
        client->RecvDataType.responseState = RequestResponseState(client->sid,client->recvBuf,client->recvSize);
    }else{
        client->RecvDataType.responseState = Diag_RESPONSE_ERR;
            return Diag_ERR_TIMEOUT;
    }
    return Diag_OK;
}

//----------------------------------------AC08-------------------------------------------------------
//--------------BMS-----------------------
Diag_SquenceUDSType AC08_BMS_ShakeHandSquence[10] = {
   {Diag_0X10_SendDiagSessCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{Diag_ExtendedDiagnostic}},
   {Diag_0X27_SendSecurityAccessFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_Request_0x27_CallBack_AC8,3,{0x01,(uint32_t)NULL,0}},
   {Diag_0X27_SendSecurityAccessFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,3,{0x02,(uint32_t)UDSKeyValue_0x27,4}},
//    {Diag_0X22_SendRDBIFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,2,{(uint32_t)AC08_BMS_ReadVersion,1}},
   {Diag_0X31_SendRoutineCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,4,{Diag_StartRoutine,0x0203,(uint32_t)NULL,0}},
   {Diag_0X10_SendDiagSessCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{Diag_ExtendedDiagnostic}},
   {Diag_0X85_DTCControlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{Diag_DtcOff}},
   {Diag_0X28_SendCommCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,2,{Diag_DisableRxAndTx,Diag_NormalCommunicationMessages}},
   {Diag_0X10_SendDiagSessCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{Diag_ProgrammingSession}},
   {Diag_0X27_SendSecurityAccessFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_Request_0x27_CallBack_AC8,3,{0x09,(uint32_t)NULL,0}},
   {Diag_0X27_SendSecurityAccessFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,3,{0x0a,(uint32_t)UDSKeyValue_0x27,4}},
};

Diag_SquenceUDSListType AC08_BMS_ShakeHandSquenceList = {
    .SquenceNodeListHead = AC08_BMS_ShakeHandSquence,
    .listSize = 10,
    .ret = Diag_OK,
    .currentItem = 0,
    .client = &UDSClient,
    .ecuType = UDS_BMS,
    .phyId = 0x101,
    .responseId = 0x601,
};

Diag_SquenceUDSType AC08_BMS_ContinueSquence[2] = {
   {Diag_0X31_SendRoutineCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,4,{Diag_StartRoutine,0xf001,(uint32_t)AC08_BMS_RoutineCtrlMiddleData,COUNT_OF_TABLE(AC08_BMS_RoutineCtrlEndData)}},
   {Diag_0X31_SendRoutineCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,4,{Diag_StartRoutine,0xff00,(uint32_t)AC08_BMS_RoutineCtrlContinueData,COUNT_OF_TABLE(AC08_BMS_RoutineCtrlContinueData)}},
};

Diag_SquenceUDSListType AC08_BMS_ContinueSquenceList = {
    .SquenceNodeListHead = AC08_BMS_ContinueSquence,
    .listSize = 2,
    .ret = Diag_OK,
    .currentItem = 0,
    .client = &UDSClient,
    .ecuType = UDS_BMS,
    .phyId = 0x101,
    .responseId = 0x601,
};


Diag_SquenceUDSType AC08_BMS_DeShakeHandSquence[4] = {
   {Diag_0X31_SendRoutineCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{Diag_StartRoutine,0xf001,(uint32_t)AC08_BMS_RoutineCtrlEndData,COUNT_OF_TABLE(AC08_BMS_RoutineCtrlEndData)}},
   {Diag_0X31_SendRoutineCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_Request_SteTime_CallBack_AC8,1,{Diag_StartRoutine,0xff01,(uint32_t)NULL,0}},
   {Diag_0X2E_SendWDBIFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,3,{0xf199,(uint32_t)updateTime,4}},
   {Diag_0X11_SendEcuResetFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{0x01}},
};

Diag_SquenceUDSListType AC08_BMS_DeShakeHandSquenceList = {
    .SquenceNodeListHead = AC08_BMS_DeShakeHandSquence,
    .listSize = 4,
    .ret = Diag_OK,
    .currentItem = 0,
    .client = &UDSClient,
    .ecuType = UDS_BMS,
    .phyId = 0x101,
    .responseId = 0x601,
};
/*-------------------------VCU------------------------*/
Diag_SquenceUDSType AC08_VCU_ShakeHandSquence[7] = {
   {Diag_0X10_SendDiagSessCtrlFunc,Diag_REQ_IDEL,Diag_FUNC_ID,Diag_RequestCommCallBack,1,{Diag_ExtendedDiagnostic}},
   {Diag_0X85_DTCControlFunc,Diag_REQ_IDEL,Diag_FUNC_ID,Diag_RequestCommCallBack,1,{0x82}},
   {Diag_0X28_SendCommCtrlFunc,Diag_REQ_IDEL,Diag_FUNC_ID,Diag_RequestCommCallBack,2,{0x81,Diag_NormalCommunicationMessages}},
   {Diag_0X10_SendDiagSessCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{Diag_ProgrammingSession}},
   {Diag_0X27_SendSecurityAccessFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_Request_0x27_CallBack_AC8,3,{0x01,(uint32_t)NULL,0}},
   {Diag_0X27_SendSecurityAccessFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,3,{0x02,(uint32_t)UDSKeyValue_0x27,4}},
   {Diag_0X31_SendRoutineCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,4,{Diag_StartRoutine,0xff00,(uint32_t)AC08_VCU_RoutineCtrlData,COUNT_OF_TABLE(AC08_VCU_RoutineCtrlData)}},
};

Diag_SquenceUDSListType AC08_VCU_ShakeHandSquenceList = {
    .SquenceNodeListHead = AC08_VCU_ShakeHandSquence,
    .listSize = 7,
    .ret = Diag_OK,
    .currentItem = 0,
    .client = &UDSClient,
    .ecuType = UDS_VCU,
    .phyId = 0x7e0,
    .responseId = 0x7e8
    ,
};
Diag_SquenceUDSType AC08_VCU_DeShakeHandSquence[9] = {
   {Diag_0X31_SendRoutineCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,4,{Diag_StartRoutine,0xf001,(uint32_t)AC08_VCU_RoutineDeCtrlData,COUNT_OF_TABLE(AC08_VCU_RoutineDeCtrlData)}},
   {Diag_0X31_SendRoutineCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,4,{Diag_StartRoutine,0xff01,(uint32_t)NULL,0}},
   {Diag_0X11_SendEcuResetFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{0x01}},
   {Diag_0X10_SendDiagSessCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{Diag_ExtendedDiagnostic}},
   {Diag_0X85_DTCControlFunc,Diag_REQ_IDEL,Diag_FUNC_ID,Diag_RequestCommCallBack,1,{Diag_DtcOn}},
   {Diag_0X28_SendCommCtrlFunc,Diag_REQ_IDEL,Diag_FUNC_ID,Diag_RequestCommCallBack,2,{Diag_EnableRxAndTx,Diag_NormalCommunicationMessages}},
   {Diag_0X27_SendSecurityAccessFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_Request_0x27_CallBack_AC8,3,{0x01,(uint32_t)NULL,0}},
   {Diag_0X27_SendSecurityAccessFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,3,{0x02,(uint32_t)UDSKeyValue_0x27,4}},
   {Diag_0X2E_SendWDBIFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,3,{0xf199,(uint32_t)updateTime,4}},

};
Diag_SquenceUDSListType AC08_VCU_DeShakeHandSquenceList = {
    .SquenceNodeListHead = AC08_VCU_DeShakeHandSquence,
    .listSize = 9,
    .ret = Diag_OK,
    .currentItem = 0,
    .client = &UDSClient,
    .ecuType = UDS_VCU,
    .phyId = 0x7e0,
    .responseId = 0x7e8,
};
//---------------------------------------------------------------------------------------------------

//-----------------------------------------OBC-----------------------------------------------------

Diag_SquenceUDSType AC08_OBC_ShakeHandSquence[9] = {
//   {Diag_0X10_SendDiagSessCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{Diag_ExtendedDiagnostic}},
//   {Diag_0X27_SendSecurityAccessFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_Request_0x27_CallBack_AC8,3,{0x01,(uint32_t)NULL,0}},
//   {Diag_0X27_SendSecurityAccessFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,3,{0x02,(uint32_t)UDSKeyValue_0x27,4}},
   {Diag_0X10_SendDiagSessCtrlFunc,Diag_REQ_IDEL,Diag_FUNC_ID,Diag_RequestCommCallBack,1,{Diag_ExtendedDiagnostic}},
   {Diag_0X85_DTCControlFunc,Diag_REQ_IDEL,Diag_FUNC_ID,Diag_RequestCommCallBack,1,{Diag_DtcOff}},
   {Diag_0X28_SendCommCtrlFunc,Diag_REQ_IDEL,Diag_FUNC_ID,Diag_RequestCommCallBack,2,{0x03,Diag_NormalCommunicationMessages}},
   {Diag_0X31_SendRoutineCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,2,{Diag_StartRoutine,0x0203}},
   {Diag_0X10_SendDiagSessCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{Diag_ProgrammingSession}},
   {Diag_0X27_SendSecurityAccessFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_Request_0x27_CallBack_AC8,1,{0x09}},
   {Diag_0X27_SendSecurityAccessFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_Request_SteTime_CallBack_AC8_OBC,3,{0x0a,(uint32_t)UDSKeyValue_0x27,4}},
   {Diag_0X2E_SendWDBIFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_Request_HeadSize_CallBack_AC8_OBC,3,{0xf199,(uint32_t)updateTime,4}},
   {Diag_0X31_SendRoutineCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,4,{Diag_StartRoutine,0xff00,(uint32_t)AC08_OBC_RoutineCtrlContinueData,COUNT_OF_TABLE(AC08_OBC_RoutineCtrlContinueData)}},

};

Diag_SquenceUDSListType AC08_OBC_ShakeHandSquenceList = {
    .SquenceNodeListHead = AC08_OBC_ShakeHandSquence,
    .listSize = 9,
    .ret = Diag_OK,
    .currentItem = 0,
    .client = &UDSClient,
    .ecuType = UDS_OBC,
    .phyId = 0x113,
    .responseId = 0x613,
};


Diag_SquenceUDSType AC08_OBC_DeShakeHandSquence[3] = {
    {Diag_0X31_SendRoutineCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,4,{Diag_StartRoutine,0xf001,(uint32_t)AC08_OBC_RoutineCtrlContinueSecondData,4}},
    {Diag_0X31_SendRoutineCtrlFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,2,{Diag_StartRoutine,0xff01}},
    {Diag_0X11_SendEcuResetFunc,Diag_REQ_IDEL,Diag_PHY_ID,Diag_RequestCommCallBack,1,{0x01}},

};

Diag_SquenceUDSListType AC08_OBC_DeShakeHandSquenceList = {
    .SquenceNodeListHead = AC08_OBC_DeShakeHandSquence,
    .listSize = 3,
    .ret = Diag_OK,
    .currentItem = 0,
    .client = &UDSClient,
    .ecuType = UDS_OBC,
    .phyId = 0x113,
    .responseId = 0x613,
};

Diag_SquenceUDSListType *Diag_SquenceUDSListCfg[10] = {
    [AC08_BMS_START] = &AC08_BMS_ShakeHandSquenceList,
    [AC08_BMS_CONTINUE] = &AC08_BMS_ContinueSquenceList,
    [AC08_BMS_END] = &AC08_BMS_DeShakeHandSquenceList,
    [AC08_VCU_START]=&AC08_VCU_ShakeHandSquenceList,
    [AC08_VCU_END]=&AC08_VCU_DeShakeHandSquenceList,
    [AC08_OBC_START] = &AC08_OBC_ShakeHandSquenceList,
    [AC08_OBC_END] = &AC08_OBC_DeShakeHandSquenceList,
};

#endif

